package com.scap.authen.controller;

import java.util.LinkedHashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.scap.authen.service.LoginService;
import com.scap.authen.service.RoleService;
import com.scap.authen.service.SiteService;


@Controller
@RequestMapping(value = "/")
public class LoginController {
	
	@Autowired
	private RoleService voRoleService ;
	@Autowired
	private SiteService voSiteService ;
	
	 @RequestMapping(method = RequestMethod.GET)
	 public ModelAndView login(){ 

		 return new ModelAndView("index");
	 }
	 
	 @RequestMapping(value = "index",method = RequestMethod.GET)
	 public ModelAndView index(){ 

		 return new ModelAndView("applicationManagement");
	 }
	 
	 @RequestMapping(value = "option",method = RequestMethod.GET)
	 public ModelAndView option(){ 

		 return new ModelAndView("optionManagement");
	 }
	 
	 @RequestMapping(method = RequestMethod.POST)
	 public ModelAndView verifyLogin(HttpServletRequest request,
				HttpServletResponse response) throws Exception{ 
		 
		 LoginService voLoginService = new LoginService();

		 if(voLoginService.verifyFirstLogin(request,response)){

			return new ModelAndView("redirect:index");

		 }else{
			 return new ModelAndView("index");
		 }
		 
	 }
	 
	 @ModelAttribute("vaSite")
		public Map<String,String> siteList() {
			
			//Data referencing for java skills list box
			Map<String,String> voSite = new LinkedHashMap<String,String>();
			voSite = voSiteService.getSiteList();
			
			return voSite;
		}
	 
	 @ModelAttribute("vaRole")
		public Map<String,String> roleList() {
			
			//Data referencing for java skills list box
			Map<String,String> voRole = new LinkedHashMap<String,String>();
			voRole = voRoleService.getRoleList();
			
			return voRole;
		}
	 
}
